package org.xg.security;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.xg.domain.AuthorityVO;
import org.xg.domain.RoleVO;
import org.xg.domain.UserVO;
import org.xg.service.AuthorityBS;
import org.xg.service.RoleBS;
import org.xg.service.UserBS;

public class SysUserDetailService implements UserDetailsService {
	
	public SysUserDetailService(){
		System.out.println("SysUserDetailService 构造函数");
	}
	
	@PostConstruct
	public void dd(){
		System.out.println("SysUserDetailService @PostConstruct");
	}
	
	@Resource
	private UserBS userBS;
	
	@Resource
	private AuthorityBS authBS;
	
	@Resource
	private RoleBS roleBS;
	
	/**
	 * 获取登录用户信息
	 */
	public UserDetails loadUserByUsername(String loginName)
			throws UsernameNotFoundException, DataAccessException {

		UserVO user=null;
		try {
			user = userBS.findUserByUserName(loginName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (user == null) {
			throw new UsernameNotFoundException("用户ID" + loginName + "不存在");
		}
		if ( user.isValid()!=null && !user.isValid()) {
			throw new DisabledException("该用户不可用");
		}
		AuthUser auser = new AuthUser();
		auser.setUserId(user.getId());
		auser.setUsername(user.getUsername());
		auser.setCname(user.getUserCname());
		auser.setPassword(user.getPassword());
		auser.setEmail(user.getEmail());
		auser.setPhone(user.getPhone());
		
		List<GrantedAuthority> galist = new ArrayList<GrantedAuthority>();
		
		List<AuthorityVO> authList1 = authBS.findAuthsByUsername(user.getUsername());
		addGrantedAuthoritys(galist, authList1);
		
		List<RoleVO> roles = roleBS.findRolesByUsername(user.getUsername());
		for (int i = 0; i < roles.size(); i++) {
			RoleVO role = roles.get(i);
			List<AuthorityVO> auth_ts = authBS.findAuthsByRoleid(role.getId());
			addGrantedAuthoritys(galist, auth_ts);
		}
		
		auser.setAuthorities(galist);
//		List<RoleVO> roles = userBS.loadRolesByUserName(loginName);
//		List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
//		List<String> roleCodes=new ArrayList<String>();
//		for (RoleVO role : roles) {
//			roleCodes.add(role.getRoleCode());
//		}
//		GrantedAuthority cau = new GrantedAuthorityImpl("ROLE_SMCUSER");
//		auths.add(cau);
//		auser.setAuthorities(auths);
//		auser.setRoleCodes(roleCodes);
		return auser;
	}
	
	private void addGrantedAuthoritys(List<GrantedAuthority> galist, List<AuthorityVO> authList1){
		for (AuthorityVO auth : authList1) {
			GrantedAuthority ga = new SimpleGrantedAuthority(auth.getAuthCode());
			if( !galist.contains(ga) ){ //不包含则加入
				galist.add(ga);
			}
		}
	}

}
